package org.backend.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.backend.pojo.entity.Message;
import org.backend.pojo.vo.MessageVO;
import org.backend.service.MessageService;
import org.backend.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static org.backend.utils.JwtUtil.getOpenId;
import static org.backend.utils.JwtUtil.setOpenId;

@RestController
@RequestMapping("message")
@Slf4j
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 微信小程序端的获取历史消息
     * @param request
     * @return
     */
    @PostMapping("/getHistoryMessageToMiniProgram")
    public ApiResponse<List<MessageVO>> getHistoryMessageToMiniProgram(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String openid = getOpenId(token);
        List<MessageVO> list = messageService.getHistoryMessageToMiniProgram(openid);
        return ApiResponse.success(list);
    }

}
